package com.esunny.resourcevisualization.controller;

import com.esunny.resourcevisualization.domain.entity.Apply;
import com.esunny.resourcevisualization.domain.request.ApplyInfoDto;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.service.ApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName ApplyInfoController
 * @Description 申请信息处理
 * @Author baochencong
 * @Date 2020/11/27 10:29
 */

@Controller
@RequestMapping("/apply")
public class ApplyInfoController {

    @Resource
    private ApplyService applyService;

    @ResponseBody
    @PostMapping("/addApplyInfo")
    public ReturnStructure addApplyInfo(@RequestBody ApplyInfoDto applyInfoDto) {
        if (judgeApplyInfoIsEmpty(applyInfoDto)) {
            return ReturnStructure.NOT_FOUND();
        }
        return applyService.addApplyInfo(applyInfoDto);
    }

    @ResponseBody
    @PostMapping("/updateApplyInfo")
    public ReturnStructure updateApplyInfo(@RequestBody ApplyInfoDto applyInfoDto) {
        if (judgeApplyInfoIsEmpty(applyInfoDto)) {
            return ReturnStructure.NOT_FOUND();
        }
        return applyService.updateApplyInfo(applyInfoDto);
    }

    @ResponseBody
    @PostMapping("/remarkDeleteApplyInfoById")
    public ReturnStructure remarkDeleteApplyInfoById(@RequestParam("applyId") int applyId) {
        if (applyId == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        return applyService.remarkDeleteApplyInfoById(applyId);
    }

    @ResponseBody
    @GetMapping("/getApplyListInfoByApplyBy/{applyBy}")
    public ReturnStructure getApplyListInfoByApplyBy(@PathVariable("applyBy") String applyBy) {
        if (StringUtils.isBlank(applyBy)) {
            return ReturnStructure.NOT_FOUND();
        }
        List<Apply> applyListInfo = applyService.getApplyListInfoByApplyBy(applyBy);
        return ReturnStructure.SUCCESS("获取信息成功", applyListInfo);
    }

    @ResponseBody
    @GetMapping("/getApplyInfoList")
    public ReturnStructure getApplyInfoList(){
        return ReturnStructure.SUCCESS("获取申请信息列表成功", applyService.getApplyInfoList());
    }

    @ResponseBody
    @GetMapping("/getApplyListInfoByAuditor")
    public ReturnStructure getApplyListInfoByAuditor() {
        return ReturnStructure.SUCCESS("获取信息成功", applyService.getApplyListInfoByAuditor());
    }

    @ResponseBody
    @GetMapping("/getApplyInfoById")
    public ReturnStructure getApplyInfoById(@RequestParam("applyId") int applyId) {
        if (applyId == 0) {
            return ReturnStructure.NOT_FOUND();
        }
        Apply applyInfo = applyService.getApplyInfoById(applyId);
        return ReturnStructure.SUCCESS("获取信息成功", applyInfo);
    }

    /**
     * 判断申请信息是否为空
     *
     * @param applyInfoDto 申请信息
     * @return 是否为空
     */
    private boolean judgeApplyInfoIsEmpty(ApplyInfoDto applyInfoDto) {
        if (applyInfoDto == null || applyInfoDto.getApplyInfo() == null) {
            return true;
        }
        return applyInfoDto.getApplyInfo().getApplyResourceId() == 0 && applyInfoDto.getResourceCacheInfo() == null;
    }

}
